package com.fractalwrench.harmonybootstrap.sample;

import com.beardedhen.harmonybootstrap.BootstrapProgressBar;
import com.beardedhen.harmonybootstrap.BootstrapProgressBarGroup;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.ShapeElement;

import java.security.SecureRandom;
import java.util.Optional;

public class BootstrapProgressBarGroupExample extends AbilitySlice implements Component.ClickedListener {

    BootstrapProgressBarGroup groupAdd;
    BootstrapProgressBar bootstrapProgressBar1;
    BootstrapProgressBar bootstrapProgressBar2;
    Button fillProgress;
    Button addSomething;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);

        ComponentContainer rootLayout = (ComponentContainer) LayoutScatter.getInstance(this).
            parse(ResourceTable.Layout_example_bootstrap_progress_bar_group, null, false);

        bootstrapProgressBar1 = (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_example_progress_bar_group_progress_1);
        bootstrapProgressBar2 = (BootstrapProgressBar) rootLayout.findComponentById(ResourceTable.Id_example_progress_bar_group_progress_2);
        groupAdd = (BootstrapProgressBarGroup) rootLayout.findComponentById(ResourceTable.Id_example_progress_bar_group_add_group);

        addSomething = (Button) rootLayout.findComponentById(ResourceTable.Id_example_progress_bar_group_add);
        setShapeElement(addSomething);
        fillProgress = (Button) rootLayout.findComponentById(ResourceTable.Id_example_progress_bar_group_progress);
        setShapeElement(fillProgress);

        addSomething.setClickedListener(this);
        fillProgress.setClickedListener(this);
        setUIContent(rootLayout);

    }

    private void onClickProgressChange(){
        SecureRandom rand = new SecureRandom();
        int progress = rand.nextInt(30) + 10;
        switch(rand.nextInt(2)){
            case 0:
                bootstrapProgressBar1.setProgress(progress);
                break;
            case 1:
                bootstrapProgressBar2.setProgress(progress);
                break;
        }

    }

    private void addToGroup(){
        SecureRandom rand = new SecureRandom();
        BootstrapProgressBar bar = new BootstrapProgressBar(this, new AttrSet() {
            @Override
            public Optional<String> getStyle() {
                return Optional.empty();
            }

            @Override
            public int getLength() {
                return 0;
            }

            @Override
            public Optional<Attr> getAttr(int i) {
                return Optional.empty();
            }

            @Override
            public Optional<Attr> getAttr(String s) {
                return Optional.empty();
            }
        });
        bar.setProgress(10);
        int brand = 5;
        while (brand == 5) {
            brand = rand.nextInt(7);
        }
        bar.setBootstrapBrand(DefaultBootstrapBrand.fromAttributeValue(brand));

        if(groupAdd.getCumulativeProgress() + 10 <= 90) {
            groupAdd.addComponent(bar);
        }else{
            groupAdd.removeComponents(2, groupAdd.getChildCount() - 3);
        }
    }

    private void setShapeElement(Button b) {
        b.setBackground(new UiUtil(getApplicationContext()).getShapeElement(ShapeElement.RECTANGLE, ResourceTable.Color_material_lighter_gray, 5.0f));
    }

    @Override
    public void onClick(Component component) {

        switch (component.getId()) {
            case ResourceTable.Id_example_progress_bar_group_add:
                addToGroup();
                break;
            case ResourceTable.Id_example_progress_bar_group_progress:
                onClickProgressChange();
                break;
        }

    }
}
